package com.dg.river.core.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.dg.river.core.view.WebViewSlowlyProgressBar;
import com.dg.river.module.base.BaseActivity;


/**
 * Created by hy on 2018/11/7
 * Description : WebView工具类
 */
public class WebViewUtils {

    private static WebViewSlowlyProgressBar slowlyProgressBar;

    /**
     * webView的设置
     *
     * @param webView
     */
    public static void webSetting(Context mContext, WebView webView, boolean cache) {
        WebSettings settings = webView.getSettings();
        //解决Https和http混合
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(2);
        }
        settings.setJavaScriptEnabled(true);
        // 开启 DOM storage API 功能
        settings.setDomStorageEnabled(true);
        //开启 database storage API 功能
        settings.setDatabaseEnabled(true);

        String cacheDirPath = mContext.getFilesDir().getAbsolutePath() + "zsxqh_cache";
        //设置  Application Caches 缓存目录
        settings.setAppCachePath(cacheDirPath);
        //开启 Application Caches 功能
        settings.setAppCacheEnabled(true);

        if (checkNetWorkState(mContext)) {
            if (!cache) {
                settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
            } else {
                settings.setCacheMode(WebSettings.LOAD_DEFAULT);
            }
        } else {
            settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        }
    }

    /**
     * web是否可以返回
     *
     * @param activity
     * @param webView
     * @return
     */
    public static boolean webCanGoBack(BaseActivity activity, WebView webView) {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            activity.finishAty();
        }
        return true;
    }

    /**
     * 显示webView的进度条
     *
     * @param progressBar
     * @param webView
     */
    public static void webShowProgressBar(ProgressBar progressBar, WebView webView) {
        slowlyProgressBar = new WebViewSlowlyProgressBar(progressBar);
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                if (slowlyProgressBar != null) {
                    slowlyProgressBar.onProgressStart();
                }
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (slowlyProgressBar != null) {
                    slowlyProgressBar.onProgressChange(newProgress);
                    if (newProgress == 100) {
                        slowlyProgressBar.destroy();
                        slowlyProgressBar = null;
                    }
                }
            }
        });
    }


    /**
     * 检查网络状态
     *
     * @param mContext
     */
    private static boolean checkNetWorkState(final Context mContext) {
        boolean networkAvailable = NetUtils.isNetworkAvailable(mContext);
        if (!networkAvailable) {
            return false;
        } else {
            return true;
        }
    }
}
